package com.four.dailymanage.dish.service;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.four.dailymanage.common.bean.Page;
import com.four.dailymanage.dish.dao.DishDao;
import com.four.dailymanage.dish.entity.Dish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishService {
    @Autowired
    DishDao dishDao;
    //增加
    public  boolean addD(Dish dish){
        if (dish ==null){
            return false;
        }
        return dishDao.insertDish(dish)>0;
    }
    //删除
    public  boolean  del(Integer id){
        if (id <0 || id == null){
            return false;
        }
        return dishDao.deleteDishById(id)>0;
    }
    //删除多个
    public boolean delIds(List<Integer> ids) {
        if (null==ids ||ids.size()==0){
            return false;
        }
        return dishDao.deletes(ids)>0;
    }
    //分页形式的查询
    public Page<Dish> getPageDish (int pageNum, int pageSize,String dishName,String dishType,String publish, String publishDt,
                                                 String assignDt){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Dish> list=dishDao.getDishByCon(start,pageSize,dishName,dishType,publish,publishDt,assignDt);
        //获取总条目数
        int total=dishDao.countDish(dishName, dishType, publish, publishDt, assignDt);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Dish> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //修改
    public boolean updateD(Dish dish){
        if (dish==null){
            return false;
        }
        return dishDao.update(dish)>0;
    }

}
